package cn.accp.bookShop.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import cn.accp.bookShop.dao.BookDao;
import cn.accp.bookShop.entity.Book;

public class FileBookDao implements BookDao {
	public FileBookDao() {
		String dir = this.getClass().getClassLoader().getResource("").getPath();
		this.file = new File(dir, fileName);
		try {
			this.load();
		} catch (ClassNotFoundException | IOException e) {
			this.items = new ArrayList<Book>();
		}
	}

	private static String fileName = "books.dat";
	private File file;
	private List<Book> items;

	private int indexOf(int id) {
		int index = -1;
		for (int i = 0; i < items.size(); i++) {
			if (items.get(i).getId() == id) {
				index = i;
				break;
			}
		}
		return index;
	}

	private void save() throws IOException {
		FileOutputStream fos = new FileOutputStream(file);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(items);
		fos.flush();
		oos.close();
		fos.close();
	}

	@SuppressWarnings("unchecked")
	private void load() throws IOException, ClassNotFoundException {
		if (!file.exists()) {
			file.createNewFile();
		}
		FileInputStream fis = null;
		ObjectInputStream ois = null;

		if (file.length() > 0) {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			items = (ArrayList<Book>) ois.readObject();
		}
		if (ois != null) {
			ois.close();
		}
		if (fis != null) {
			fis.close();
		}
	}

	@Override
	public boolean add(Book book) {
		if (indexOf(book.getId()) == -1) {
			items.add(book);
			try {
				this.save();
				return true;
			} catch (IOException e) {
				return false;
			}
		} else {
			return false;
		}
	}

	@Override
	public List<Book> getList() {
		try {
			this.load();
		} catch (ClassNotFoundException | IOException e) {
			this.items = new ArrayList<Book>();
		}
		return items;
	}

	@Override
	public Book getSingle(int id) {
		try {
			this.load();
		} catch (ClassNotFoundException | IOException e) {
			this.items = new ArrayList<Book>();
		}
		int index = indexOf(id);
		if (index == -1) {
			return null;
		} else {
			return items.get(index);
		}
	}

	@Override
	public boolean remove(int id) {
		try {
			this.load();
		} catch (ClassNotFoundException | IOException e) {
			this.items = new ArrayList<Book>();
		}
		int index = indexOf(id);
		if (index == -1) {
			return false;
		} else {
			items.remove(index);
			try {
				this.save();
				return true;
			} catch (IOException e) {
				return false;
			}
		}
	}

	@Override
	public boolean update(Book book) {
		try {
			this.load();
		} catch (ClassNotFoundException | IOException e) {
			this.items = new ArrayList<Book>();
		}
		int index = indexOf(book.getId());
		if (index == -1) {
			return false;
		} else {
			items.set(index, book);
			try {
				this.save();
				return true;
			} catch (IOException e) {
				return false;
			}
		}
	}
}
